package com.example.javaEE.service;

import com.example.javaEE.model.Book;
import com.example.javaEE.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookRepository bookRepository;

    public List<Book> getAllBooks() {
        return bookRepository.findAll();
    }

    public Book addBook(Book book) {
        return bookRepository.save(book);
    }

    public Book updateBook(Long id, Book book) {
        Book existingBook = bookRepository.findById(id).orElse(null);
        if (existingBook != null) {
            existingBook.setName(book.getName());
            existingBook.setAuthor(book.getAuthor());
            existingBook.setPublisher(book.getPublisher());
            existingBook.setPrice(book.getPrice());
            existingBook.setCategory(book.getCategory());
            return bookRepository.save(existingBook);
        }
        return null;
    }

    public void deleteBook(Long id) {
        bookRepository.deleteById(id);
    }

    public List<Book> searchBooksByName(String name) {
        return bookRepository.findByNameContaining(name);
    }

    public List<Book> searchBooksByAuthor(String author) {
        return bookRepository.findByAuthorContaining(author);
    }

    public List<Book> searchBooksByPublisher(String publisher) {
        return bookRepository.findByPublisherContaining(publisher);
    }

    public List<Book> searchBooksByAuthorAndPublisher(String author, String publisher) {
        return bookRepository.findByAuthorContainingAndPublisherContaining(author, publisher);
    }

    public List<Book> searchBooksByNameAuthorPublisher(String name, String author, String publisher) {
        return bookRepository.findByNameContainingAndAuthorContainingAndPublisherContaining(name, author, publisher);
    }

    public List<Book> searchBooksByNameAndAuthor(String name, String author) {
        return bookRepository.findByNameContainingAndAuthorContaining(name, author);
    }

    public List<Book> searchBooksByNameAndPublisher(String name, String publisher) {
        return bookRepository.findByNameContainingAndPublisherContaining(name, publisher);
    }
}